package com.zdkj.subway.st.util;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.admin.api.entity.SysDictItem;
import com.zdkj.admin.service.impl.SysDictItemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.util
 * @ClassName: GenSerialNo
 * @Description:使用Redis生成流水号
 * @Author: kangjian
 * @CreateDate: 2020/2/8 9:30
 * @Version: 1.0
 */
@Service
public class GenSerialNo {
    @Autowired
    SysDictItemServiceImpl sysDictItemServiceImpl;

    @Autowired
    CacheService cacheService;

    /**
     * 根据数据字典获取序列号前缀
     * @param type
     * @param dictvalue
     * @return
     */
    public synchronized String genSerioNo(String type,String dictvalue) throws Exception{
        SysDictItem dictItem=new SysDictItem();
        dictItem.setType(type);
        dictItem.setValue(dictvalue);
        SysDictItem sysDictItem=sysDictItemServiceImpl.getOne(Wrappers.query(dictItem));
        if(sysDictItem==null||sysDictItem.getRemarks()==null)
        {
            throw new RuntimeException("数据字典配置参数异常,错误参数为:type="+type+",dictvalue="+dictvalue);
        }
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Long num = cacheService.getIncrementNum(type+"_"+currentDate);
        String flowCode = SequenceUtils.getSequence(num);
        return sysDictItem.getRemarks()+currentDate+flowCode;
    }
    /**
     * 直接获取流水序列号
     * @param type
     * @return
     */
    public synchronized String genSerioNo(String type){
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Long num = cacheService.getIncrementNum(type + "_"+currentDate);
        String flowCode = SequenceUtils.getSequence(num);
        return currentDate+flowCode;
    }
    /**
	  根据车票的票种大类，获取出入库单号的前缀
	*/
	public synchronized String  getPreFixByTicketType(String ticketType){
        if("DCP".equals(ticketType) || "PTDCP".equals(ticketType) ||  "YZDCP".equals(ticketType)){
		  return "D";
		}else if("YJZP".equals(ticketType)){
		  return "Z";
		}else if("YXCP".equals(ticketType)){
		  return "Y";
		}else if("GWP".equals(ticketType)){
		  return "G";
		}		
		return "";	
	}

    /**
     * 根据月计划获取单号前缀
     * @param planType
     * @return
     */
    public synchronized String  getPreFixByPlanType(String planType){
        switch (planType){
            case "Z1":
                planType = "ZQ";//正式清洗
                break;
            case "Z2":
                planType = "ZS";//正式生产
                break;
            case "Z3":
                planType = "ZP";//正式配送
                break;
            case "Z4":
                planType = "ZH";//正式回收
                break;
            case "L1":
                planType = "LQ";//临时清洗
                break;
            case "L2":
                planType = "LS";//临时生产
                break;
            case "L3":
                planType = "LP";//临时配送
                break;
            case "L4":
                planType = "LH";//临时回收
                break;
        }
        return planType;
    }

    /**
     * 根据计划类型定义key
     * @param planType
     * @return
     */
    public synchronized String  getPlanTypeKey(String planType){
        switch (planType){
            case "Z1":
                planType = "z_q_plan_type";//正式清洗
                break;
            case "Z2":
                planType = "z_s_plan_type";//正式生产
                break;
            case "Z3":
                planType = "z_p_plan_type";//正式配送
                break;
            case "Z4":
                planType = "z_h_plan_type";//正式回收
                break;
            case "L1":
                planType = "l_q_plan_type";//临时清洗
                break;
            case "L2":
                planType = "l_s_plan_type";//临时生产
                break;
            case "L3":
                planType = "l_p_plan_type";//临时配送
                break;
            case "L4":
                planType = "l_h_plan_type";//临时回收
                break;
        }
        return planType;
    }


    /**
     * 生成单号
     * @param planType
     * @return
     */
    public String generateNum(String planType) {
        String prefix = getPreFixByPlanType(planType);
        String serialno = genSerioNo(getPlanTypeKey(planType));
        return prefix+serialno;
    }

    /**
     * 生成单号取消月和日
     * @param planType
     * @return
     */
    public String generateNewNum(String planType) {
        String prefix = getPreFixByPlanType(planType);
        String serialno = genSerioNoYear(getPlanTypeKey(planType));
        return prefix+serialno;
    }

    /**
     * 直接获取流水序列号 只获取年
     * @param type
     * @return
     */
    public synchronized String genSerioNoYear(String type){
        String currentDate = new SimpleDateFormat("yyyy").format(new Date());
        Long num = cacheService.getIncrementNumYear(type + "_"+currentDate);
        String flowCode = SequenceUtils.getSequence4(num);
        return currentDate+flowCode;
    }
}
